<!--
 * @Description:车辆查询
 * @Autor: 刘诗文
 * @Date: 2021-01-08 10:54:49
 * @LastEditors: 马毅
 * @LastEditTime: 2021-04-17 15:15:01
-->
<template>
  <div>
    <TopFilter :list="topFilterList" :cols="4" :labelWidth="105" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      ref="table"
      selectionType="single"
      :isSelectColumn="false"
      :columns="columns"
      :fetchapi="fetchapi"
      :params="fetchParams"
      :datakey="'rows'"
      columnsRef="vehicleQueryOEMlsw"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="onRowSelectChange"
    >
      <template slot="controls">
        <AjaxButton size="small" type="primary" icon="el-icon-download" :click="vehicleExportData">导出</AjaxButton>
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="actions.visible" :title="actions.title" width="300" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <searchDearlerList :resData="this.resData" @dealerData="dealerData" @close="closeSetting('dealer')" />
    </BaseDialog>
    <Drawer :visible.sync="vehicleActions.visible" :title="vehicleActions.title" width="70%" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <vehicleQueryPanel :formData="this.resData" @close="closeSetting('vehicle')" />
    </Drawer>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { exportExcelFile } from '@/utils/index';
import { getPinpSelect, getChexSelect, getCheModelSelect } from '@/api/repair/repairReception/customer/brand.js';
import { getWxlxSelect, getSfqfSelect } from '@/api/repair/repairReception/customer';
import { getVehicleInfoOEM, getVehicleExcelOEMList } from '@/api/customerRepair/customerInfo/ownerVehicleManagement';
import { getDearlerListByTranslate } from '@/api/customerRepair/vehicleOwnerManagement/vehicleChange';
import moment from 'moment';
import searchDearlerList from '@/pages/customerRepair/vehicleOwnerManagement/vehicleChangeQuery/searchDearlerList';
import vehicleQueryPanel from './vehicleQueryPanel';

export default {
  name: 'vehicleQueryOEM',
  components: {
    searchDearlerList,
    vehicleQueryPanel
  },
  mixins: [dictionary],
  data() {
    this.selectedData = null;
    this.BaseTable = null;
    this.resData = null;
    return {
      topFilterList: this.createTopFilterList(),
      columns: this.createTableColumns(),
      fetchapi: getVehicleInfoOEM,
      fetchParams: { pageNum: 1, xhrAbort: true },
      actions: { visible: false, title: '', type: '' },
      vehicleActions: { visible: false, title: '', type: '' },
      dealerList: [],
      dealerCode: '' // 经销商代码
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table;
    // 字典值查询
    this.initDictCode();
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin'
        },
        {
          type: 'SELECT',
          label: '品牌',
          fieldName: 'brand',
          itemList: [],
          change: val => {
            this.getSeries(val);
          }
        },
        {
          type: 'SELECT',
          label: '车系',
          fieldName: 'series',
          itemList: [],
          change: val => {
            this.getModel(val);
          }
        },
        {
          type: 'SELECT',
          label: '车型',
          fieldName: 'model',
          itemList: []
        },
        {
          type: 'INPUT',
          label: '车牌号',
          fieldName: 'license'
        },
        // {
        //   type: 'SELECT',
        //   label: '车辆状态',
        //   fieldName: 'vehicleStatus',
        //   itemList: this.createDictList(6040)
        // },
        {
          type: 'SELECT',
          label: '动力形式',
          fieldName: 'powerType',
          itemList: this.createDictList(6008)
        },
        {
          type: 'INPUT',
          label: '经销商',
          fieldName: 'dealerName',
          readonly: true,
          initialValue: '',
          unitRender: props => {
            return <el-button icon="el-icon-search" onClick={() => this.openHandler('dealer', {})} />;
          }
        },
        {
          type: 'RANGE_DATE',
          label: '实销日期',
          fieldName: 'salesDateBegin|salesDateEnd',
          initialValue: [
            moment(new Date())
              .subtract(6, 'month')
              .format('YYYY-MM-DD'),
            moment(new Date()).format('YYYY-MM-DD')
          ]
        },
        // {
        //   type: 'RANGE_DATE',
        //   label: '维修日期',
        //   fieldName: 'repairDateBegin|repairDateEnd'
        // },
        {
          type: 'SELECT',
          label: '维修类型',
          fieldName: 'repairTypeCode',
          itemList: []
        },
        // {
        //   type: 'SELECT',
        //   label: '索赔类型',
        //   fieldName: 'claimType',
        //   itemList: this.createDictList(5501)
        // },
        {
          type: 'INPUT',
          label: '客户姓名',
          fieldName: 'ownerName',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '客户手机',
          fieldName: 'mobile'
        },
        {
          type: 'INPUT',
          label: '送修人姓名',
          fieldName: 'deliverer'
        },
        {
          type: 'INPUT',
          label: '送修人电话',
          fieldName: 'delivererPhone'
        },
        {
          type: 'INPUT',
          label: '活动代码',
          fieldName: 'activityCode'
        },
        {
          type: 'INPUT',
          label: '活动名称',
          fieldName: 'activityName'
        }
        // {
        //   type: 'INPUT',
        //   label: '优惠券码',
        //   fieldName: 'a1'
        // }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          // sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 70,
          render: props => {
            return (
              <div style="text-align:center">
                <el-button size="mini" type="text" onClick={() => this.openHandler('detail', props.row)}>
                  明细
                </el-button>
              </div>
            );
          }
        },
        {
          title: '销售经销商代码',
          dataIndex: 'saleOwnerNo',
          showOverflowTooltip: true
        },
        {
          title: '销售经销商名称',
          dataIndex: 'dealerName',
          showOverflowTooltip: true,
          render: props => {
            return <span>{this.getText(props.row.saleOwnerNo)}</span>;
          }
        },
        {
          title: '动力形式',
          dataIndex: 'powerType',
          showOverflowTooltip: true,
          dictItems: this.createDictList(6008)
        },
        {
          title: 'VIN码',
          dataIndex: 'vin',
          showOverflowTooltip: true
        },
        {
          title: '车系名称',
          dataIndex: 'seriesName',
          showOverflowTooltip: true
        },
        {
          title: '车型名称',
          dataIndex: 'modelName',
          showOverflowTooltip: true
        },
        // {
        //   title: '订单类型',
        //   dataIndex: 'a1',
        //   showOverflowTooltip: true
        // },
        {
          title: '生产日期',
          dataIndex: 'productDate',
          showOverflowTooltip: true
        },
        {
          title: '实销日期',
          dataIndex: 'salesDate',
          showOverflowTooltip: true
        }
      ];
    },
    changeHandle(val) {
      if (val.dealerName === '') {
        this.dealerCode = val.ownerCode = '';
      } else {
        val.ownerCode = this.dealerCode;
      }
      if (!val.license && !val.vin && !val.salesDateBegin && !val.salesDateEnd) {
        return this.$notify.warning({ title: '提示信息', message: 'vin,车牌号,实销日期必填其一' });
      }
      if (val.salesDateBegin || val.salesDateEnd) {
        let diffMonth = moment(val.salesDateEnd).diff(moment(val.salesDateBegin), 'months');
        if (diffMonth > 11) return this.$notify.warning({ title: '提示信息', message: '实销起止日期跨度不能超过一年' });
      }
      this.fetchParams = Object.assign({ xhrAbort: false }, val);
    },
    collapseHandle() {
      this.$nextTick(this.BaseTable.EXECUTE_RESET_HEIGHT);
    },
    onRowSelectChange(row) {
      if (!row.length) {
        this.selectedData = null;
      } else {
        this.selectedData = row[0];
      }
    },
    initDictCode() {
      // 获取品牌
      this.getBrand();
      // 获取维修类型
      this.queryRepairTypeDict();
      // 获取经销商代码
      this.getDearlerListByTranslate();
    },
    // 品牌
    async getBrand() {
      const res = await getPinpSelect();
      this.topFilterList.find(item => item.fieldName === 'brand').itemList = res.data.map(x => ({
        value: x.brandId + '',
        text: x.brandName
      }));
    },
    // 车系
    async getSeries(val) {
      const res = await getChexSelect(val);
      this.topFilterList.find(item => item.fieldName === 'series').itemList = res.data.map(x => ({ value: x.seriesId + '', text: x.seriesName }));
    },
    // 车型
    async getModel(seriessid) {
      let brand = this.topFilterList.find(item => item.fieldName === 'brand').initialValue;
      const res = await getCheModelSelect(brand, seriessid);
      this.topFilterList.find(item => item.fieldName === 'model').itemList = res.data.map(x => ({ value: x.modelId + '', text: x.modelName, model: x.modelCode }));
    },
    // 打开窗口
    openHandler(type, val) {
      if (type === 'dealer') {
        this.actions = { visible: true, title: '经销商查询', type: 'search' };
      } else if (type === 'detail') {
        this.vehicleActions = { visible: true, title: '车辆明细', type: 'show' };
        this.resData = val;
      }
    },
    // 获取经销商数据
    dealerData(val) {
      this.topFilterList.find(x => x.fieldName === 'dealerName').initialValue = val.dealerName;
      this.dealerCode = val.dealerCode;
      this.closeSetting('dealer');
    },
    // 关闭经销商页面
    closeSetting(type) {
      if (type === 'dealer') {
        this.actions.visible = false;
      } else if (type === 'vehicle') {
        this.vehicleActions.visible = false;
      }
    },
    // 获取维修类型
    async queryRepairTypeDict() {
      let res = await getWxlxSelect();
      if (res.resultCode == 200) {
        let repairTypeDict = res.data.map(item => ({ text: item.REPAIR_TYPE_NAME, value: item.REPAIR_TYPE_CODE + '' }));
        this.topFilterList.find(x => x.fieldName === 'repairTypeCode').itemList = repairTypeDict;
      }
    },
    // 查询全部经销商列表
    async getDearlerListByTranslate() {
      const res = await getDearlerListByTranslate();
      if (res.resultCode === 200) {
        if (res.errMsg) {
          notifyAction(res.errMsg, 'error');
        } else {
          this.dealerList = res.data;
        }
      }
    },
    // 翻译经销商列表
    getText(val) {
      return this.dealerList.find(x => x.dealerCode === val) ? this.dealerList.find(x => x.dealerCode === val).dealerName : '';
    },
    // 车辆导出
    async vehicleExportData() {
      await getVehicleExcelOEMList(this.fetchParams).then(res => {
        console.log(res);
        exportExcelFile(res.data, '车辆信息');
      });
    }
  }
};
</script>
<style lang="less" scoped></style>
